<!-- 招标者登录和注册 -->
<script setup>
import { ref } from 'vue';
import { ElForm, ElFormItem, ElInput, ElButton, ElCard } from 'element-plus';
import { ElMessage } from 'element-plus'


const islogin=ref(true)

// 表单数据模型
const registerDate = ref({
  username: '',
  password: '',
  confirmPassword: '',
  companyName: '',
  email: '',
  address: '',
});
  // 表单数据模型
  const loginDate = ref({
    username: '',
    password: '',
  });

// 验证确认密码一致性
const validateConfirmPassword = (rule, value, callback) => {
  if (value !== registerDate.value.password) {
    callback(new Error('两次输入的密码不一致'));
  } else {
    callback();
  }
};

// 表单验证规则
const rules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符之间', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ],
  companyName: [
    { required: true, message: '请输入投标单位名称', trigger: 'blur' },
  ],
  email: [
    { required: true, message: '请输入联系邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入有效的邮箱地址', trigger: 'blur' }
  ],
  address: [
    { required: true, message: '请输入地址', trigger: 'blur' },
  ],
});
const rules2 = ref({
    username: [
      {required: true,message: '请输入用户名', trigger: 'blur' },
    ],
    password: [
      {required: true,message: '请输入密码', trigger: 'blur' },
    ],

  });

import { biddingResgisterService,biddingLogin,getbidder} from '@/api/bidding.js';

// 提交表单
const register = async () => {
  let result = await biddingResgisterService(registerDate.value);
  if (result.code === 200) {
    ElMessage.success("注册成功");
  } else {
    ElMessage.error("注册失败");
  }
};
import userInfoStore from '@/stores/userInfo.js'
import { useRouter } from 'vue-router';

const router =useRouter();
const userStore=userInfoStore();
import useTokenStore from '@/stores/token.js';
const tokenStore = useTokenStore();

const login = async () => {
    let result = await biddingLogin(loginDate.value);
    if (result.code === 200) {
      ElMessage.success("登入成功");
      tokenStore.setToken(result.data)
      let info= await getbidder(loginDate.value.username)

      userStore.setInfo(info.data);

      router.push({path:'bidderIndex'})
    } else {
      ElMessage.error("登入失败");
    }
  };
</script>

<template>
    <el-dialog
    v-model="dialogVisible"
    width="500px"
    align-center
  >
    <el-main>
      <el-card class="login-card" v-if="islogin">
          <h2 class="title">招标用户登入</h2>
          <el-form :model="loginDate" ref="formRef" :rules="rules2" label-width="110px" @submit.prevent="login">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="loginDate.username" placeholder="请输入用户名"></el-input>
            </el-form-item>

            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="loginDate.password" placeholder="请输入密码"></el-input>
            </el-form-item>

            <el-form-item>
              <el-button @click="islogin=false">注册</el-button>
              <el-button type="primary" @click="login">登入</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      <el-card class="register-card" v-else>
        <h2 class="title">招标用户注册</h2>
        <el-form :model="registerDate" ref="formRef" :rules="rules" label-width="110px" @submit.prevent="register">

          <el-form-item label="用户名" prop="username">
            <el-input v-model="registerDate.username" placeholder="请输入用户名"></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="registerDate.password" placeholder="请输入密码"></el-input>
          </el-form-item>

          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input type="password" v-model="registerDate.confirmPassword" placeholder="请确认密码"></el-input>
          </el-form-item>

          <el-form-item label="投标单位名称" prop="companyName">
            <el-input v-model="registerDate.companyName" placeholder="请输入投标单位名称"></el-input>
          </el-form-item>

          <el-form-item label="联系邮箱" prop="email">
            <el-input v-model="registerDate.email" placeholder="请输入联系邮箱"></el-input>
          </el-form-item>

          <el-form-item label="地址" prop="address">
            <el-input v-model="registerDate.address" placeholder="请输入地址"></el-input>
          </el-form-item>

          <el-form-item>
              <el-button @click="islogin=true">返回</el-button>
              <el-button type="primary" @click="register">注册</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-main>
</el-dialog>
</template>

<style scoped>
/* 整体布局 */
.register-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
  overflow-x: hidden; /* 确保不出现水平滚动条 */
}

/* 卡片样式 */
.el-card {
  width: 420px;
  max-width: 100%; /* 确保在小屏幕上不会溢出 */
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.05);
  background-color: white;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.el-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1);
}

/* 标题样式 */
.title {
  text-align: center;
  margin-bottom: 24px;
  color: #303133;
  font-size: 20px;
  font-weight: 500;
}

/* 表单样式 */
.el-form {
  margin-top: 16px;
}

.el-form-item {
  margin-bottom: 18px;
}

.el-form-item__label {
  color: #606266;
  font-weight: 400;
}

/* 输入框样式 */
.el-input {
  border-radius: 6px;
  height: 40px;
  transition: border-color 0.2s ease;
}

.el-input:hover {
  border-color: #409EFF;
}

.el-input:focus-within {
  border-color: #409EFF;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
}

/* 按钮样式 */
.el-button {
  margin-top: 12px;
  height: 40px;
  border-radius: 6px;
  font-weight: 500;
  transition: all 0.2s ease;
}

.el-button[type="primary"] {
  background-color: #409EFF;
  border-color: #409EFF;
}

.el-button[type="primary"]:hover {
  background-color: #337ab7;
  border-color: #337ab7;
  transform: translateY(-1px);
}

.el-button[type="default"] {
  background-color: white;
  border-color: #dcdfe6;
  color: #606266;
}

.el-button[type="default"]:hover {
  background-color: #f5f7fa;
  border-color: #c0c4cc;
  transform: translateY(-1px);
}

/* 验证码按钮 */
.captcha-btn {
  width: 100%;
  border-radius: 6px;
  background-color: #409EFF;
  color: white;
  height: 40px;
  transition: all 0.2s ease;
}

.captcha-btn:hover {
  background-color: #337ab7;
  transform: translateY(-1px);
}

/* 错误提示样式 */
.el-form-item__error {
  padding-top: 4px;
  font-size: 12px;
  color: #f56c6c;
  transition: all 0.2s ease;
}

/* 表单切换动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 确保dialog不会导致滚动条 */
.el-dialog {
  max-width: 100vw;
  overflow-x: hidden;
}

/* 确保main区域不会导致滚动条 */
.el-main {
  overflow-x: hidden;
}
</style>

